package type.fragment.Adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import type.fragment.TypeFragment;
import type.fragment.ThirdLevelCategory;
import com.example.xfengshopping.R;

public class ThirdLevelAdapter extends RecyclerView.Adapter<ThirdLevelAdapter.ViewHolder> {
    private Context context;
    private List<ThirdLevelCategory> thirdLevelCategoryList;
    private OnItemClickListener itemClickListener;

    public interface OnItemClickListener {
        void onItemClick(ThirdLevelCategory category);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public ThirdLevelAdapter(Context context, List<ThirdLevelCategory> list) {
        this.context = context;
        this.thirdLevelCategoryList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_third_category, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ThirdLevelCategory thirdCategory = thirdLevelCategoryList.get(position);
        holder.tvThirdCategory.setText(thirdCategory.getCategoryName());

        // 使用 Glide 加载图标（需要添加依赖）
        if (thirdCategory.getIconUrl() != null && !thirdCategory.getIconUrl().isEmpty()) {
            Glide.with(context)
                    .load(thirdCategory.getIconUrl())
                    .placeholder(R.drawable.default_category_icon)
                    .error(R.drawable.default_category_icon)
                    .into(holder.ivCategoryIcon);
        }

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(thirdCategory);
            }
        });
    }

    @Override
    public int getItemCount() {
        return thirdLevelCategoryList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCategoryIcon;
        TextView tvThirdCategory;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCategoryIcon = itemView.findViewById(R.id.iv_category_icon);
            tvThirdCategory = itemView.findViewById(R.id.tv_third_category);
        }
    }
}